﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using XMLGener.Interfaces;
using XMLGener.Utils;

namespace XBasicModules
{
    /// <summary>
    /// 
    /// </summary>
    public class OutWebService : ModuleBase, XMLGenerOutputInterface
    {

        private static string definition =  "<function>" +
             "    <name>Web services</name>" +
             "    <id>webservice</id>" +
             "    <attributes>" +
             "       <attribute>" +
             "          <name>Endpoint url</name>" +
             "          <id>url</id>" +
             "          <type>text</type>" +
             "       </attribute>" +
             "       <attribute>" +
             "          <name>SOAPAction</name>" +
             "          <id>action</id>" +
             "          <type>text</type>" +
             "       </attribute>" +
             "    </attributes>" +
             "</function>";

        private void sendRequestFromFile(string inputFile, string outputFile)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.ContentType = "text/xml;charset=UFT-8";
            request.Headers.Add("SOAPAction", action);
            request.Method = "POST";

            request.KeepAlive = false;

            Stream aStream = request.GetRequestStream();
            StreamWriter aStreamWriter = new StreamWriter(aStream);
            StringBuilder aStringBuilder = new StringBuilder();
            XmlDataDocument myDoc = new XmlDataDocument();
            myDoc.Load(inputFile);
            XmlElement manEl = myDoc.DocumentElement;

            aStringBuilder.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" >")
               .Append("   <soapenv:Header/>")
               .Append("   <soapenv:Body>");
            aStringBuilder.Append(manEl.OuterXml);

            aStringBuilder.Append("   </soapenv:Body>")
                .Append("</soapenv:Envelope");

            aStreamWriter.Write(aStringBuilder);
            aStreamWriter.Close();
            WebResponse response = request.GetResponse();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

            StreamReader readStream = new StreamReader(response.GetResponseStream(), encode);
            Char[] read = new Char[1024];

            int count = readStream.Read(read, 0, 1024);
            TextWriter tw = new StreamWriter(outputFile);
            while (count > 0)
            {
                String str = new String(read, 0, count);
                tw.Write(str);
                count = readStream.Read(read, 0, 1024);
            }
            tw.Close();
            readStream.Close();


        
        }

        String url = null;
        String action = null;
        String method = null;

        #region XMLGenerOutputInterface Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputFile"></param>
        /// <param name="outputFile"></param>
        public void generateOutput(string inputFile, string outputFile)
        {
            sendRequestFromFile(inputFile, outputFile);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        public void processOutput(string fileName)
        { 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getDefinition()
        {
            return definition;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlParams"></param>
        public void setParameters(string xmlParams)
        {
            if (!this.checkSettingsElement(xmlParams,null))
                return;

            this.url = this.getParam("url");
            this.action = this.getParam("action");
            this.method = this.getParam("method");

        }

        #endregion
    }
}
